package com.cdwm.mrp.controller.api;

import com.cdwm.mrp.common.tools.LayuiResponse;
import com.cdwm.mrp.common.tools.ResultVO;
import com.cdwm.mrp.domain.po.InterfaceDoc;
import com.cdwm.mrp.domain.po.Project;
import com.cdwm.mrp.service.InterfaceDocService;
import com.cdwm.mrp.service.ProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/3
 */
@Api(tags = "项目管理")
@RestController
@RequestMapping("/api")
public class ProjectController {

    @Autowired
    private ProjectService projectService;




    @ApiOperation("添加项目信息")
    @PostMapping("/project/save")
    public ResultVO<?> saveProject(@RequestBody Project project) {
        return projectService.save(project)? ResultVO.ok(): ResultVO.fail();
    }


    @ApiOperation("删除项目信息")
    @PostMapping("/project/delete/{id}")
    public ResultVO<?> deleteProject(@PathVariable Long id) {
        return projectService.removeById(id)? ResultVO.ok(): ResultVO.fail();
    }

    /**
     * 批量删除用户
     * @param id
     * @return
     */
    @ApiOperation("批量删除项目信息")
    @PostMapping("/project/batchRemove/{id}")
    public ResultVO<?> batchRemove(@PathVariable Long[] id){
        boolean removeStatus = projectService.removeByIds(Arrays.asList(id));
        if (removeStatus){
            return  ResultVO.ok();
        }
        return  ResultVO.fail();
    }


    @ApiOperation("修改项目信息")
    @PostMapping("/project/update")
    public ResultVO<?> updateProject(@RequestBody Project project) {
        return projectService.updateById(project)? ResultVO.ok(): ResultVO.fail();
    }

    @ApiOperation("查询项目信息")
    @GetMapping("/project/list")
    public LayuiResponse<?> listProject( Project project,
                                        @RequestParam(required = false,defaultValue = "1") Integer page,
                                        @RequestParam(required = false,defaultValue = "5") Integer limit
                                   ) {
        return projectService.getProjectList(project,page,limit);
    }


    @PostMapping("/project/upload/{id}")
    public ResultVO<?> fileupload(MultipartFile file, HttpServletRequest req,@PathVariable Integer id) throws Exception {

        Boolean bool = projectService.uploadInterfaceWord(file, req, id);

        return bool ?  ResultVO.ok("上传成功") : ResultVO.fail("上传失败");
    }
}
